//<SCRIPT>
/* min/max toggle and drag globals */
var titleBarClipHeight = 14;
// initialize global position-tracking variables
var drag_currentX = 0;
var drag_currentY = 0;
var drag_curDragEl = null;   // initialize global element-being-dragged variable
var drag_lastDragEl = null;   // initialize global element-being-dragged variable
var drag_origMouseMoveHandler = null
// don't let items be dragged out of view
var drag_constrainDrag = true;
var drag_origHeight = -1;
var drag_origWidth = -1;
var drag_startStopCallbacks = [];
function drag_addStartStopCallback ( callback, dragElem /*,optional more elems*/ )
{
	if ( typeof( callback ) == "function" )
	{
		for ( var i = 1; i < arguments.length; i++ )
		{
			var elem = arguments[ i ];
			if ( isObj( elem ) )
			{
				drag_startStopCallbacks[ elem.id ] = callback ;
			}
		}
	}
}	// 
function drag_removeStartStopCallback ( dragElem )
{
	for ( var i = 0; i < arguments.length; i++ )
	{
		var elem = arguments[ i ];
		if ( isObj( elem ) && drag_startStopCallbacks[ elem.id ])
		{
			delete drag_startStopCallbacks[ elem.id ];
		}
	}
}	// 
// can only be called during or after the containing page's onload() handler
function drag_init ( callback, dragElem )
{

	drag_addStartStopCallback( callback, dragElem );
	/* Initialize drag variables and event handlers */
	//from xblib.js: function addEvent ( obj, evType, fn, useCapture, Nav4EventCode )
	if ( isIE )
	{
		// cancel the (ie only) selectstart event if we're dragging
		addEvent( document, "selectstart", drag_onSelectStart );
	}
	addEvent( document, "mousedown", drag_startDrag, true, "Event.MOUSEDOWN" );
	addEvent( document, "mouseup", drag_stopDrag, true, "Event.MOUSEUP" );
}
function drag_getDragableElem ( evt )
{
	/*
		is the event target an input control? - do not allow dragging
		cheap way to detect input objects:
		all input objects have a 'form' property which points to the 
		parent form object
	*/
	var elem = (isIE ? evt.srcElement : evt.target);
	if ( (!elem) || elem.form )
		return null ;
	if ( ! isNav4 )
	{
		while ( elem )
		{
			if ( elem.id && (-1 != elem.id.indexOf( "DRAG" )) )
			{
				break;
			}
			else
			{
				elem = elem.parentElement;
			}
		}
		return elem ;
	}
	else	// nav4
	{
        var mouseX = evt.pageX;
        var mouseY = evt.pageY;
        for ( var i=0; i<document.layers.length; i++ )
        {
	        tempLayer = document.layers[i];
            if ( tempLayer.id.indexOf( "DRAG" ) == -1 ) { continue }
            if ( (mouseX > tempLayer.left) && 
                 (mouseX < (tempLayer.left + tempLayer.clip.width)) && 
                 (mouseY > tempLayer.top) && 
                 (mouseY < (tempLayer.top + tempLayer.clip.height)) )
            {
                return tempLayer;
            }
		}
		return null;
	}
}
// function for onmousedown
function drag_startDrag ( e )
{
    drag_curDragEl = drag_getDragableElem( (isIE ? window.event : e) );
	if ( ! drag_curDragEl ) return ;
	if ( isIE )
	{
	    drag_curDragEl.style.pixelLeft = drag_curDragEl.offsetLeft;
	    drag_curDragEl.style.pixelTop = drag_curDragEl.offsetTop;
	    drag_currentX = (event.clientX + document.body.scrollLeft);
	    drag_currentY = (event.clientY + document.body.scrollTop);
		drag_origHeight = drag_curDragEl.clientHeight;
		drag_origWidth = drag_curDragEl.clientWidth;
	}
	else
	{										 
        drag_currentX = e.pageX;
        drag_currentY = e.pageY;
		drag_origHeight = drag_curDragEl.clip.height;
		drag_origWidth = drag_curDragEl.clip.width;
    }
	if ( drag_startStopCallbacks[ drag_curDragEl.id ] )
	{
		var func = drag_startStopCallbacks[ drag_curDragEl.id ];
		if ( typeof( func ) == "function" )
		{
			if ( func( true, e, drag_curDragEl ) )
			{
				return;
			}
		}
	}

	// if multiple draggable elements are on a page, make sure the
	// currently dragged elem is on top
	if (drag_lastDragEl && (drag_curDragEl != drag_lastDragEl))
	{

        if ( isIE )
        {
			drag_curDragEl.style.zIndex = drag_lastDragEl.style.zIndex + 1;
        }
		else
		{
			drag_curDragEl.moveAbove( drag_lastDragEl );
		}
    }
    drag_lastDragEl = drag_curDragEl;
	// capture the mouse move event
	drag_origMouseMoveHandler = document.onmousemove;
	document.onmousemove = drag_drag;
	if ( isNav4 )
	{
		document.captureEvents( Event.MOUSEMOVE );
	}
}	// drag_startDrag
// function for onmousemove
function drag_drag ( e )
{
    if ( drag_curDragEl == null )
    { 
    	//keep bubbling the event - it's not ours
    	return ;
    }
	var newX, newY;
	var tmpLeft, tmoTop;
	if ( isIE )
	{
	    newX = event.clientX + document.body.scrollLeft;
	    newY = event.clientY + document.body.scrollTop;
		if ( drag_constrainDrag )
		{
			//constrain movement so the dragged element can't be dragged off screen
		    tmpLeft = Math.max(0, Math.min(
		    	document.body.clientWidth + document.body.scrollLeft - drag_origWidth, 
		    	drag_curDragEl.style.pixelLeft + newX - drag_currentX));
		    tmpTop = Math.max(0, Math.min(
		    	document.body.clientHeight + document.body.scrollTop - drag_origHeight,
		    	drag_curDragEl.style.pixelTop + newY - drag_currentY));
		}
		else
		{
		    tmpLeft = drag_curDragEl.style.pixelLeft + newX - drag_currentX;
		    tmpTop = drag_curDragEl.style.pixelTop + newY - drag_currentY;
		}
		drag_curDragEl.style.pixelLeft = tmpLeft;
		drag_curDragEl.style.pixelTop = tmpTop;
	}
	else
	{
		// form elements get repainted incorrectly when being moved
		// so we hide them during move operation by setting the clipping
		// region to expose only the 'title bar'
		//drag_curDragEl.clip.bottom = titleBarClipHeight;
        newX = e.pageX + Math.max(0, window.pageXOffset - e.pageX);
        newY = e.pageY + Math.max(0, window.pageYOffset - e.pageY);
		//drag_curDragEl.moveBy( newX - drag_currentX, newY - drag_currentY );
		if ( drag_constrainDrag )
		{
			//constrain movement so the dragged element can't be dragged off screen
	    	tmpLeft = Math.max(0, Math.min(
	    		innerWidth + window.pageXOffset - drag_origWidth, 
	    		drag_curDragEl.left + newX - drag_currentX));
			tmpTop = Math.max(0, Math.min(
				innerHeight + window.pageYOffset - drag_origHeight, 
				drag_curDragEl.top + newY - drag_currentY));
		}
		else
		{
	    	tmpLeft = drag_curDragEl.left + newX - drag_currentX;
			tmpTop = drag_curDragEl.top + newY - drag_currentY;
		}
	    drag_curDragEl.moveTo( tmpLeft, tmpTop );
	}
	/*
*/
    drag_currentX = newX;
    drag_currentY = newY;
	return cancelEvent( e );
}	// drag_drag
// function for onmouseup
function drag_stopDrag ( e )
{
	if ( ! drag_curDragEl ) return ;
	if ( drag_startStopCallbacks[ drag_curDragEl.id ] )
	{
		var func = drag_startStopCallbacks[ drag_curDragEl.id ];
		if ( typeof( func ) == "function" )
		{
			func( false, e, drag_curDragEl );
		}
	}
	if ( isNav ) // expose the form
	{ 
		//drag_curDragEl.clip.bottom = drag_origHeight;
		document.releaseEvents( Event.MOUSEMOVE );
	}
	document.onmousemove = drag_origMouseMoveHandler;
	drag_curDragEl = null;
}
// cancels text selection event in IE
function drag_onSelectStart ()
{
    if ( drag_curDragEl != null ) 
    { 
    	return cancelEvent( window.event );
    }
}
/*
//swap in 'MOVE' cursor when mouse is over draggable element (IE only)
function cursEl ()
{
    if (event.srcElement.id.indexOf("DRAG") != -1)
    {
        event.srcElement.style.cursor = "move"
    }
}
*/
//</SCRIPT>

